package com.ruoyi.project.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.examination.util.SnowflakeIdUtils;
import com.ruoyi.project.common.A08TypeCommon;
import com.ruoyi.project.vo.A08EditListRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.A08PaymentReceiptMapper;
import com.ruoyi.project.domain.A08PaymentReceipt;
import com.ruoyi.project.service.IA08PaymentReceiptService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 收付款信息数据Service业务层处理
 * 
 * @author WorrilessGo
 * @date 2025-11-22
 */
@Service
public class A08PaymentReceiptServiceImpl implements IA08PaymentReceiptService 
{
    @Resource
    private A08PaymentReceiptMapper a08PaymentReceiptMapper;

    /**
     * 查询收付款信息数据
     * 
     * @param id 收付款信息数据主键
     * @return 收付款信息数据
     */
    @Override
    public A08PaymentReceipt selectA08PaymentReceiptById(String id)
    {
        return a08PaymentReceiptMapper.selectA08PaymentReceiptById(id);
    }

    /**
     * 查询收付款信息数据列表
     * 
     * @param a08PaymentReceipt 收付款信息数据
     * @return 收付款信息数据
     */
    @Override
    public List<A08PaymentReceipt> selectA08PaymentReceiptList(A08PaymentReceipt a08PaymentReceipt)
    {
        String projectId = a08PaymentReceipt.getProjectId();
        if (projectId == null) {
            throw new RuntimeException("项目id不能为空");
        }
        return a08PaymentReceiptMapper.selectA08PaymentReceiptList(a08PaymentReceipt);
    }

    /**
     * 新增收付款信息数据
     * 
     * @param a08PaymentReceipt 收付款信息数据
     * @return 结果
     */
    @Override
    public int insertA08PaymentReceipt(A08PaymentReceipt a08PaymentReceipt)
    {
        a08PaymentReceipt.setCreateTime(DateUtils.getNowDate());
        String id = String.valueOf(new SnowflakeIdUtils(5, 8).nextId());
        a08PaymentReceipt.setId(id);
        return a08PaymentReceiptMapper.insertA08PaymentReceipt(a08PaymentReceipt);
    }

    /**
     * 修改收付款信息数据
     * 
     * @param a08PaymentReceipt 收付款信息数据
     * @return 结果
     */
    @Override
    public int updateA08PaymentReceipt(A08PaymentReceipt a08PaymentReceipt)
    {
        a08PaymentReceipt.setUpdateTime(DateUtils.getNowDate());
        return a08PaymentReceiptMapper.updateA08PaymentReceipt(a08PaymentReceipt);
    }

    /**
     * 批量删除收付款信息数据
     * 
     * @param ids 需要删除的收付款信息数据主键
     * @return 结果
     */
    @Override
    public int deleteA08PaymentReceiptByIds(String[] ids)
    {
        return a08PaymentReceiptMapper.deleteA08PaymentReceiptByIds(ids);
    }

    /**
     * 删除收付款信息数据信息
     * 
     * @param id 收付款信息数据主键
     * @return 结果
     */
    @Override
    public int deleteA08PaymentReceiptById(String id)
    {
        return a08PaymentReceiptMapper.deleteA08PaymentReceiptById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editList(A08EditListRequest a08EditListRequest) {
        String projectId = a08EditListRequest.getProjectId();
        if (projectId == null) {
            throw new RuntimeException("项目id不能为空");
        }
        A08PaymentReceipt where = new A08PaymentReceipt();
        where.setProjectId(projectId);
        //删除旧的数据
        List<A08PaymentReceipt> listOld = a08PaymentReceiptMapper.selectA08PaymentReceiptList(where);
        if (listOld != null && listOld.size() > 0) {
            a08PaymentReceiptMapper.deleteA08PaymentReceiptByIds(listOld.stream().map(A08PaymentReceipt::getId).toArray(String[]::new));
        }
        List<A08PaymentReceipt> list = new ArrayList<>();
        SnowflakeIdUtils snowflakeIdUtils = new SnowflakeIdUtils(5, 8);
        List<A08PaymentReceipt> expenseItems = a08EditListRequest.getExpenseItems();
        for(A08PaymentReceipt one:expenseItems){
            one.setType(A08TypeCommon.EXPENSE);
            one.setId(String.valueOf(snowflakeIdUtils.nextId()));
            list.add( one);
        }
        List<A08PaymentReceipt> incomeItems = a08EditListRequest.getIncomeItems();
        for (A08PaymentReceipt one:incomeItems){
            one.setType(A08TypeCommon.INCOME);
            String id = String.valueOf(snowflakeIdUtils.nextId());
            one.setId(id);
            list.add( one);
        }
        return a08PaymentReceiptMapper.batchInsertA08PaymentReceipt(list);
    }
}
